package com.myweb.util;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
/**
 * @TODO 将Request中的参数封装成BEAN
 * @author roye
 * @date Apr 17, 2013 11:38:22 AM
 * @version V1.0
 */
public class RequestUtil {

	/**
	* Class<T> 目标分装类
	* @param <T>
	* @param request
	* @param beanClass
	* @return
	*/
	@SuppressWarnings({"rawtypes", "unchecked" })
	public static <T> T requestToBean(HttpServletRequest request, Class<T> beanClass) {
	   try {
			T bean = beanClass.newInstance();
			Map map = request.getParameterMap();
			//BeanUtils.populate(bean, map);
			return (T)RequestUtil.convertToBean(map,bean);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	 /** 
     * 将request中的所有参数设置到entityClass类型的对象上 
     * @param entityClass 
     * @param request 
     * @return 
     */  
    @SuppressWarnings("rawtypes")
	public static Object copyParam(Class entityClass,HttpServletRequest request){  
          
        try {  
            Object entity = entityClass.newInstance();  
              
            //把请求中的参数取出  
            Map allParams = request.getParameterMap();  
            Set entries = allParams.entrySet();  
            for (Iterator iterator = entries.iterator(); iterator.hasNext();) {  
                Map.Entry entry = (Map.Entry) iterator.next();  
                String name = (String)entry.getKey();  
                String[] value = (String[])entry.getValue();  
                if(value != null){  
                    if(value.length == 1){  
                        BeanUtils.copyProperty(entity, name, value[0]);  
                    }else{  
                        BeanUtils.copyProperty(entity, name, value);  
                    }  
                }  
            }  
            return entity;  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
          
        return null;  
    }  
    
    /**
     * 
     * 功能描述:  将Map值转化成bean
     * @throws IllegalAccessException 
     * @throws InvocationTargetException 
     * @参数  
     * @返回值 Object
     * @作者 : 张琳 zhanglin@cdeledu.com
     * @创建日期 : 2013-4-2下午05:24:20
     *
     */
    public static Object convertToBean(Map<String, String> map, Object obj) throws IllegalAccessException, InvocationTargetException {  
        Class class1 = obj.getClass();  
        Method[] methods = class1.getMethods();  
        for (Method method : methods) {  
            String methodName = method.getName();  
            if (methodName.startsWith("set")) {  
                String propertyName = methodName.substring(3).toUpperCase(  
                        Locale.getDefault());  
                String value = map.get(propertyName);  
                try {  
                    method.invoke(obj, value);  
                } catch (IllegalArgumentException e) {  
                    throw e;  
                } catch (IllegalAccessException ex) {  
                	throw ex;  
                } catch (InvocationTargetException exx) {  
                	throw exx; 
                }  
            }  
        }  
        return obj;  
    }  
}
